/**
 * Created by zhangyu on 2017/4/13.
 */
require(['zTreeCheckbox', 'grid', 'topBar', 'layout'], function () {
    var userInfo = commonFun.execParentMethod("getUserInfo");               //当前登录用户信息
    var selCorpId = userInfo.corpId,
        corpName = "",
        editDialog,
        $sysCorpTree = $("#sysCorpTree"),
        grid = $("#grid"),
        top = $("#top");

    function initsysCorpTree(nodeId){
        var node;
        $.ajaxExt({
            url : "/sysCorpInfo/getJurisdictionCorpInfoList.htm"
        }).done(function(data){
            if(commonFun.execParentMethod('getUserInfo').roleType ==='ROLT0001'){
                if (!data) {
                    data = [];
                }
                // 树的参数配置
                var setting = {
                    data: {
                        key: {
                            name: "corpName"
                        },
                        simpleData: {
                            enable: true,
                            idKey: "corpId",
                            pIdKey: "parentCorpId",
                            rootPId: 0
                        }
                    },
                    view: {
                        showIcon: true
                    },
                    callback: {
                        onClick: sysCorpTreeClick
                    }
                };
                //生成树
                sysCorpTreeObj = $sysCorpTree.createTree(setting, data, {search: $('#searchlabel')});//$.fn.zTree.init($sysCorpTree, setting, data);
                sysCorpTreeObj.expandAll(true);
                if(typeof nodeId !== "undefined"){//选中对应节点
                    sysCorpTreeObj.setting.view.expandSpeed = "";
                    sysCorpTreeObj.setting.view.expandSpeed = "fast";
                    node = sysCorpTreeObj.getNodesByFilter(function(node) { return node.corpId == nodeId; }, true);
                }else{//选中第一个节点
                    node = sysCorpTreeObj.getNodesByFilter(function(node) { return node.level == 0; }, true);
                }
                sysCorpTreeObj.selectNode(node,false);
                sysCorpTreeClick(null,node.corpId,node,true);
            }else{
                $("#left").hide();
                $("#corpCd").attr("readonly", true);
                $("#enabledSt0").attr("disabled", true);
                $("#enabledSt1").attr("disabled", true);
                $("#corpType").attr("disabled", true);
            }
        });
    }

    /**
     * 点击树节点
     */
    function sysCorpTreeClick(event, treeId, treeNode,clickFlag){
        selCorpId = treeNode.corpId;
        corpName = treeNode.corpName;
    }

    function topBarInit() {
        top.topBar({
            left: [
                {
                    text: '车辆型号',
                    name: 'carModelName',
                    width: 150,
                    type: 'text'
                },
                {
                    text: '查询',
                    type: 'btn',
                    click: function () {
                        reloadGridData();
                    }
                }
            ],
            right: [
                {
                    text: '导出',
                    type: 'export-btn',
                    authority: '',
                    item: [
                        {
                            text: 'Excel导出',
                            click: function () {
                                exportFile('Excel');
                            }
                        },
                        {
                            text: 'PDF导出',
                            click: function () {
                                exportFile('Pdf');
                            }
                        }
                    ]
                },
                {
                    text: '删除',
                    type: 'btn',
                    width: '698',
                    authority: '',
                    click: function () {
                        deleteCarModel(true);
                    }
                },
                {
                    text: '新增',
                    type: 'btn',
                    width: '698',
                    authority: '',
                    click: function () {
                        getEditDialog('新增').dialog('exec', 'reset', corpName);
                    }
                }

            ]
        });
    }

    grid.grid({
        url: "/baseCarModelInfo/getCarModelInfoPage.htm",
        multiselect: true,
        height: $(window).height() -  100,
        colModel: [
            {name: "id", label: 'id', key: true, hidden: true},
            {name: "carModelName", label: '车辆型号', width: 40},
            {name: "remark", label: '备注'},
            // {name: "enableSt", label: '状态', width: 100},
            {name: "operate", width: 40, label: '操作', title: false,align:'center',
                formatter: 'operArea',
                formatoptions: [{
                    text: '删除',
                    css:'operate-delete',
                    click: function (data) {
                        deleteCarModel(false, data.id);
                    }
                }]
            }
        ]
    });


    function deleteCarModel(isBatch, id) {
        var objIds = id;
        if(isBatch){
            objIds =  grid.grid('getGridParam', 'selarrrow').toString();
            if(objIds == ""){
                $.messager.alert("提示", "请至少选择一条数据!");
                return false;
            }
        }
        $.messager.confirm('删除', '确定删除数据吗？', 'delete', function (r) {
            if (r) {
                $.ajaxExt({
                    async:true,
                    url : "/baseCarModelInfo/delCarModelInfo.htm",
                    data : {'ids' : objIds}
                }).done(function(){
                    reloadGridData()
                });
            }
        });
    }

    function reloadGridData(){
        var param = top.getValues();
        // if("ROLT0001" == userInfo.roleType){
        // }else{
        // }
        param.corpId = selCorpId;
        grid.grid('setGridParam', {
            datatype: "JSON",
            page: 1,
            postData: param
        }).trigger("reloadGrid");
    }

    function exportFile(suffix){
        if(grid.getGridParam("reccount")>0){
            var postData=top.parent().getValues();
            postData.corpId = selCorpId;
            $.exportFile("/baseCarModelInfo/exportCarModelInfo"+suffix+".htm",postData);
        } else{
            $.messager.alert("警告","请先查询数据！","info");
        }
    }

    function getEditDialog(option) {
        option = option || {};
        if (editDialog) {
            editDialog.dialog('open');
            if (option.title) {
                editDialog.dialog('setOption', 'title', option.title);
            }
        } else {
            editDialog = $('<div>').dialog({
                title: option.title || '添加车型信息',
                url: './master/setting/sysCarModelInfoSetEdit.htm',
                height: 400,
                width: 720,
                autoOpen: true,
                buttons: [
                    {
                        text: '确定',
                        css: 'mr10',
                        click: function () {
                            var _t = this;
                            $(_t).dialog('exec', 'add', selCorpId, function(){
                                reloadGridData();
                                editDialog.dialog('close');
                            });
                        }
                    },
                    {
                        text: '重置',
                        css: 'btn-cancel',
                        click: function () {
                            $(this).dialog('exec', 'reset');
                        }
                    }
                ]
            });
        }
        return editDialog;
    }

    function initHeight(){
        var height = $(window).height();
        $sysCorpTree.parent().height(height - 45);
        $sysCorpTree.height(height - 55);
        $('#right').height(height);
    }

    function init() {
        $(window).on('resize', initHeight).trigger('resize');
        $('.slide-panel').on('click', function () {
            $('#left').css('margin-left', $(this).toggleClass('slide-panel-active').hasClass('slide-panel-active') ? -250 : 0);
        });
    }

    init();
    initsysCorpTree();
    topBarInit();
    $('body').layout();
});